package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.SaleCostDAO;
import com.feeyin.radar.crm.form.SaleCostForm;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.model.SaleCost;
import com.feeyin.radar.crm.service.SaleCostService;
import com.feeyin.radar.msg.dao.WarningInfoDAO;
import com.feeyin.radar.sys.dao.DataDictionaryDAO;
import com.feeyin.radar.sys.dao.DepartmentDAO;
import com.feeyin.radar.sys.dao.RecordRangeDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.DataDictionary;
import com.feeyin.radar.sys.model.Users;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SaleCostServiceImpl
  implements SaleCostService
{
  private SaleCostDAO saleCostDAO;
  private DataDictionaryDAO dataDictionaryDAO;
  private RecordRangeDAO recordRangeDAO;
  private UserDAO userDAO;
  private DepartmentDAO departmentDAO;
  private CustomerDAO customerDAO;
  private WarningInfoDAO warningInfoDAO;

  public SaleCostDAO getSaleCostDAO()
  {
    return this.saleCostDAO;
  }

  public void setSaleCostDAO(SaleCostDAO saleCostDAO) {
    this.saleCostDAO = saleCostDAO;
  }

  public DataDictionaryDAO getDataDictionaryDAO() {
    return this.dataDictionaryDAO;
  }

  public void setDataDictionaryDAO(DataDictionaryDAO dataDictionaryDAO) {
    this.dataDictionaryDAO = dataDictionaryDAO;
  }

  public RecordRangeDAO getRecordRangeDAO() {
    return this.recordRangeDAO;
  }

  public void setRecordRangeDAO(RecordRangeDAO recordRangeDAO) {
    this.recordRangeDAO = recordRangeDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public DepartmentDAO getDepartmentDAO() {
    return this.departmentDAO;
  }

  public void setDepartmentDAO(DepartmentDAO departmentDAO) {
    this.departmentDAO = departmentDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public WarningInfoDAO getWarningInfoDAO() {
    return this.warningInfoDAO;
  }

  public void setWarningInfoDAO(WarningInfoDAO warningInfoDAO) {
    this.warningInfoDAO = warningInfoDAO;
  }

  public List getSaleCostTypeRanges() {
    List saleCostTypesRanges = new ArrayList();

    List customerDictionarys = new ArrayList();
    customerDictionarys = this.dataDictionaryDAO.getDataDictionarysByRecordType("3");
    if (customerDictionarys != null) {
      for (int i = 0; i < customerDictionarys.size(); i++) {
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary = (DataDictionary)customerDictionarys.get(i);
        List ranges = this.recordRangeDAO.getActivityRecordRangesByDictionaryId(dataDictionary.getId());
        if ("31".equals(dataDictionary.getRecordCode())) {
          saleCostTypesRanges.addAll(ranges);
        }
      }
    }
    return saleCostTypesRanges;
  }

  public List getPageDeptSaleCosts(String saleCostCode, Integer userId, Page page)
  {
    if (userId == null) return null;
    if (page == null) return null;
    SaleCost saleCost = new SaleCost();
    if (!StringUtils.isEmpty(saleCostCode)) {
      saleCost.setSaleCostCode(saleCostCode);
    }
    saleCost.setStatus("2");

    Users user = new Users();
    user = this.userDAO.getUser(userId);
    if (user == null) return null;
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (StringUtils.isEmpty(deptIds)) return null;
    saleCost.setDeptIds(deptIds);

    List saleCosts = new ArrayList();
    saleCosts = this.saleCostDAO.getPageSaleCosts(saleCost, page);
    if (saleCosts != null) {
      for (int i = 0; i < saleCosts.size(); i++) {
        saleCost = new SaleCost();
        saleCost = (SaleCost)saleCosts.get(i);
        user = new Users();
        user = this.userDAO.getUser(saleCost.getApplyPersonId());
        if (user != null) {
          saleCost.setApplyPerson(user.getUserName());
        }

        user = new Users();
        user = this.userDAO.getUser(saleCost.getAuditPersonId());
        if (user != null) {
          saleCost.setAuditPerson(user.getUserName());
        }
      }
    }
    return saleCosts;
  }

  public Integer getPageDeptSaleCostsCount(String saleCostCode, Integer userId) {
    if (userId == null) return Integer.valueOf(0);
    SaleCost saleCost = new SaleCost();
    if (!StringUtils.isEmpty(saleCostCode)) {
      saleCost.setSaleCostCode(saleCostCode);
    }
    saleCost.setStatus("2");

    Users user = new Users();
    user = this.userDAO.getUser(userId);
    if (user == null) return Integer.valueOf(0);
    String deptIds = this.departmentDAO.getCurrentDeptIdAndSubDeptIdsByDeptId(user.getDeptId());
    if (StringUtils.isEmpty(deptIds)) return Integer.valueOf(0);
    saleCost.setDeptIds(deptIds);

    return this.saleCostDAO.getPageSaleCostsCount(saleCost);
  }

  public List getPageSaleCosts(String saleCostCode, String status, Integer userId, Page page) {
    if (StringUtils.isEmpty(status)) return null;
    if (userId == null) return null;
    if (page == null) return null;
    SaleCost saleCost = new SaleCost();
    if (!StringUtils.isEmpty(saleCostCode)) {
      saleCost.setSaleCostCode(saleCostCode);
    }
    saleCost.setStatus(status);
    saleCost.setApplyPersonId(userId);

    List saleCosts = new ArrayList();
    saleCosts = this.saleCostDAO.getPageSaleCosts(saleCost, page);
    if (saleCosts != null) {
      for (int i = 0; i < saleCosts.size(); i++) {
        saleCost = new SaleCost();
        saleCost = (SaleCost)saleCosts.get(i);
        Users user = new Users();
        user = this.userDAO.getUser(saleCost.getApplyPersonId());
        if (user != null) {
          saleCost.setApplyPerson(user.getUserName());
        }

        user = new Users();
        user = this.userDAO.getUser(saleCost.getAuditPersonId());
        if (user != null) {
          saleCost.setAuditPerson(user.getUserName());
        }
      }
    }
    return saleCosts;
  }

  public Integer getPageSaleCostsCount(String saleCostCode, String status, Integer userId) {
    if (StringUtils.isEmpty(status)) return null;
    if (userId == null) return null;
    SaleCost saleCost = new SaleCost();
    if (!StringUtils.isEmpty(saleCostCode)) {
      saleCost.setSaleCostCode(saleCostCode);
    }
    saleCost.setStatus(status);
    saleCost.setApplyPersonId(userId);
    return this.saleCostDAO.getPageSaleCostsCount(saleCost);
  }

  public List getSaleCostsBySaleCostCode(String saleCostCode) {
    if (StringUtils.isEmpty(saleCostCode)) return null;
    List saleCosts = new ArrayList();
    saleCosts = this.saleCostDAO.getSaleCostsBySaleCostCode(saleCostCode);
    if (saleCosts == null) return null;
    for (int i = 0; i < saleCosts.size(); i++) {
      SaleCost saleCost = new SaleCost();
      saleCost = (SaleCost)saleCosts.get(i);

      if (saleCost.getCustomerId() != null) {
        Customer customer = new Customer();
        customer = this.customerDAO.getCustomerById(saleCost.getCustomerId());
        if (customer != null) {
          saleCost.setCustomerName(customer.getCustomerName());
        }
      }

      Users user = new Users();
      user = this.userDAO.getUser(saleCost.getApplyPersonId());
      if (user != null) {
        saleCost.setApplyPerson(user.getUserName());
      }

      user = new Users();
      user = this.userDAO.getUser(saleCost.getAuditPersonId());
      if (user != null) {
        saleCost.setAuditPerson(user.getUserName());
      }
    }
    return saleCosts;
  }

  public BigDecimal getTotalSaleCostBySaleCostCode(String saleCostCode) {
    return this.saleCostDAO.getTotalSaleCostBySaleCostCode(saleCostCode);
  }

  public boolean insertSaleCost(SaleCostForm saleCostForm) {
    if (saleCostForm == null) return false;
    if ((saleCostForm.getCostValue() == null) || (saleCostForm.getCostValue().length <= 0)) return false;
    if ((saleCostForm.getCostTypeId() == null) || (saleCostForm.getCostTypeId().length <= 0)) return false;

    for (int i = 0; i < saleCostForm.getCostValue().length; i++) {
      if ((saleCostForm.getCostValue()[i] == null) || (saleCostForm.getCostValue()[i].compareTo(new BigDecimal(0)) <= 0) || 
        (saleCostForm.getCostTypeId()[i] == null) || (saleCostForm.getCostTypeId()[i].intValue() <= 0))
        continue;
      SaleCost saleCost = new SaleCost();
      saleCost.setCostTypeId(saleCostForm.getCostTypeId()[i]);
      saleCost.setCostValue(saleCostForm.getCostValue()[i]);
      if ((saleCostForm.getCustomerId() != null) && (saleCostForm.getCustomerId().length > 0) && 
        (saleCostForm.getCustomerId()[i] != null) && (saleCostForm.getCustomerId()[i].intValue() > 0)) {
        saleCost.setCustomerId(saleCostForm.getCustomerId()[i]);
      }
      saleCost.setSaleCostCode(saleCostForm.getSaleCostCode());
      saleCost.setApplyDate(saleCostForm.getApplyDate());
      saleCost.setApplyPersonId(saleCostForm.getApplyPersonId());
      saleCost.setStatus("1");
      saleCost.setYear(DateUtils.getStringDateByFormat(new Date(), "yyyy"));
      if ((saleCostForm.getCostDesc() != null) && (saleCostForm.getCostDesc().length > 0)) {
        saleCost.setCostDesc(saleCostForm.getCostDesc()[i]);
      }
      this.saleCostDAO.insertSaleCost(saleCost);
    }

    List saleCosts = new ArrayList();
    saleCosts = this.saleCostDAO.getSaleCostsBySaleCostCode(saleCostForm.getSaleCostCode());
    if ((saleCosts != null) && (saleCosts.size() > 0)) {
      this.warningInfoDAO.insertAuditWarningInfoForSaleCost(saleCostForm.getSaleCostCode(), saleCostForm.getApplyPersonId());
    }
    return true;
  }

  public boolean updateSaleCost(SaleCostForm saleCostForm) {
    if (saleCostForm == null) return false;
    if (StringUtils.isEmpty(saleCostForm.getSaleCostCode())) return false;
    if ((saleCostForm.getCostValue() == null) || (saleCostForm.getCostValue().length <= 0)) return false;
    if ((saleCostForm.getCostTypeId() == null) || (saleCostForm.getCostTypeId().length <= 0)) return false;

    boolean result = false;
    result = this.saleCostDAO.deleteSaleCost(saleCostForm.getSaleCostCode());
    if (result) {
      for (int i = 0; i < saleCostForm.getCostValue().length; i++) {
        if ((saleCostForm.getCostValue()[i] == null) || (saleCostForm.getCostValue()[i].compareTo(new BigDecimal(0)) <= 0) || 
          (saleCostForm.getCostTypeId()[i] == null) || (saleCostForm.getCostTypeId()[i].intValue() <= 0))
          continue;
        SaleCost saleCost = new SaleCost();
        saleCost.setCostTypeId(saleCostForm.getCostTypeId()[i]);
        saleCost.setCostValue(saleCostForm.getCostValue()[i]);
        if ((saleCostForm.getCustomerId() != null) && (saleCostForm.getCustomerId().length > 0) && 
          (saleCostForm.getCustomerId()[i] != null) && (saleCostForm.getCustomerId()[i].intValue() > 0)) {
          saleCost.setCustomerId(saleCostForm.getCustomerId()[i]);
        }
        saleCost.setSaleCostCode(saleCostForm.getSaleCostCode());
        saleCost.setApplyDate(saleCostForm.getApplyDate());
        saleCost.setApplyPersonId(saleCostForm.getApplyPersonId());
        saleCost.setStatus("1");
        saleCost.setYear(DateUtils.getStringDateByFormat(new Date(), "yyyy"));
        if ((saleCostForm.getCostDesc() != null) && (saleCostForm.getCostDesc().length > 0)) {
          saleCost.setCostDesc(saleCostForm.getCostDesc()[i]);
        }
        this.saleCostDAO.insertSaleCost(saleCost);
      }

    }

    List saleCosts = new ArrayList();
    saleCosts = this.saleCostDAO.getSaleCostsBySaleCostCode(saleCostForm.getSaleCostCode());
    if ((saleCosts != null) && (saleCosts.size() > 0)) {
      this.warningInfoDAO.insertAuditWarningInfoForSaleCost(saleCostForm.getSaleCostCode(), saleCostForm.getApplyPersonId());
    }
    return true;
  }

  public boolean deleteSaleCost(String saleCostCode) {
    return this.saleCostDAO.deleteSaleCost(saleCostCode);
  }
}